import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet("/request")
public class RequestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<h3>首行部分</h3>");
        stringBuilder.append(req.getProtocol());// 得到协议名和版本号
        stringBuilder.append("<br>");
        stringBuilder.append(req.getMethod());// 得到请求的方法
        stringBuilder.append("<br>");
        stringBuilder.append(req.getRequestURI());// 得到请求的路径
        stringBuilder.append("<br>");
        stringBuilder.append(req.getContextPath());// 得到上下文路径
        stringBuilder.append("<br>");
        stringBuilder.append(req.getQueryString());// 得到请求的参数
        stringBuilder.append("<br>");

        stringBuilder.append("<h3>header部分</h3>");
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String headerName = headerNames.nextElement();
            String headerValue = req.getHeader(headerName);
            stringBuilder.append(headerName + ": " + headerValue);
            stringBuilder.append("<br>");
        }
        // 获取 body中的数据 ---  req.getInputStream()


        resp.setContentType("text/html;charset=utf8" +
                "");
        resp.getWriter().write(stringBuilder.toString());
    }
}
